//-----------------------------------------------------------------
//  Copyright 2010 Brady Wright and Above and Beyond Software
//	All rights reserved
//-----------------------------------------------------------------


using UnityEditor;
using UnityEngine;
using System.Collections;



//[CustomEditor(typeof(UIButton))]
public class lwEZUICtlEditor : UICtlEditor
{
	protected override void ShowSpriteSettings()
	{
		IPackableControl cont = (IPackableControl)control;

		// Get the info for this state/element:
		stateInfo = cont.GetStateElementInfo(curState);


		// See if the sprite timeline is available:
		if (ste == null)
		{
			System.Reflection.Assembly asm = System.Reflection.Assembly.GetAssembly(typeof(UICtlEditor));
			System.Type steType = asm.GetType("lwEZSpriteTimelineExt");
			if (steType != null)
			{
				ste = (ISTE)System.Activator.CreateInstance(steType);
				ste.Setup(position, 0);
			}
		}

		// NOW see if the timeline is available:
		if (ste != null)
		{
			ste.SetCurAnim(curState);
			needRepaint = ste.STEOnGUI(-(20f + height), out textureAreaBottom);
		}
		else
		{
			// Put up a texture drop box:
			// Load the texture:
			/*if (stateInfo.stateObj.framePaths.Length < 1)
			{
				stateInfo.tex = null;
			}
			else {
				if(!string.IsNullOrEmpty(stateInfo.stateObj.framePaths[0]) && System.IO.File.Exists(System.IO.Path.GetFullPath(stateInfo.stateObj.framePaths[0]))) {
					WWW image = new WWW("file://" + System.IO.Path.GetFullPath(stateInfo.stateObj.framePaths[0]));
					while(!image.isDone);
					stateInfo.tex = image.texture;
					statePath = System.IO.Path.GetFullPath(stateInfo.stateObj.framePaths[0]);
				}
			}*/


			BeginMonitorChanges();
#if UNITY_IPHONE && !(UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_3_6 || UNITY_3_7 || UNITY_3_8 || UNITY_3_9)
			// Draw a "clear" button:
			if(GUI.Button(new Rect(clearRect.x, clearRect.y + height, clearRect.width, clearRect.height), "X"))
				stateInfo.tex = null;

			// Select the texture for the state:
			stateInfo.tex = (Texture2D)EditorGUI.ObjectField(tempRect, stateInfo.tex, typeof(Texture2D));
			textureAreaBottom = tempRect.yMax + ctlVirtSpace;
#else
			// Select the texture for the state:
			/*if(stateInfo.tex == null)
				EditorGUILayout.LabelField("None texture is selected", GUI.skin.box, GUILayout.ExpandWidth(true));
			else
				EditorGUILayout.LabelField(new GUIContent(stateInfo.tex), GUI.skin.box, GUILayout.ExpandWidth(true));
			*/
			//if(rectTex != GUILayoutUtility.GetLastRect())
			//	rectTex = GUILayoutUtility.GetLastRect();
			textureAreaBottom = 0; // This tells us to use Layout.
#endif
			// Handle drag and drop from an outside source:
			/*EventType eventType = Event.current.type;
			
			if (eventType == EventType.DragUpdated || eventType == EventType.DragPerform)
			{
				if (rectTex.Contains(Event.current.mousePosition))
				{
					// Show a copy icon on the drag
					DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
					
					if (eventType == EventType.DragPerform)
					{
						DragAndDrop.AcceptDrag();
						WWW image = new WWW("file://" + System.IO.Path.GetFullPath(DragAndDrop.paths[0]));
						while(!image.isDone);
						stateInfo.tex = image.texture;
						statePath = System.IO.Path.GetFullPath(DragAndDrop.paths[0]);
						needRepaint = true;
					}
	
					Event.current.Use();
				}
			}*/
			
			EndMonitorChanges();
			
			// Re-assign the state info to the control:
			// If we have an available frame, assign the new path
			/*if (stateInfo.stateObj.framePaths.Length > 0)
			{
				stateInfo.stateObj.framePaths[0] = statePath;
			} // Else only create a new Frame element if the selection is non-null:
			else if (stateInfo.tex != null)
			{
				stateInfo.stateObj.framePaths = new string[1];
				stateInfo.stateObj.framePaths[0] = statePath;
			} // Else, don't do anything. Period.
			 */
		}
	}
}
